import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';

/// 图片加载器
class AppImg extends StatelessWidget {
  const AppImg({
    Key? key,
    this.width,
    this.height,
    this.fit = BoxFit.contain,
    required this.url,
    this.headers,
    this.alignment = Alignment.center,
    this.transformDuration = const Duration(milliseconds: 200),
    this.radius = 0,
    this.borderRadius,
    this.mode = ExtendedImageMode.none,
    this.enableSlideOutPage = false,
    this.heroBuilderForSlidingPage,
    this.heroTag,
    this.opacity,
    this.color,
    this.colorBlendMode,
  }) : super(key: key);

  const AppImg.fitCover({
    Key? key,
    this.width,
    this.height,
    this.fit = BoxFit.cover,
    required this.url,
    this.headers,
    this.alignment = Alignment.center,
    this.transformDuration = const Duration(milliseconds: 200),
    this.radius = 0,
    this.borderRadius,
    this.mode = ExtendedImageMode.none,
    this.enableSlideOutPage = false,
    this.heroBuilderForSlidingPage,
    this.heroTag,
    this.opacity,
    this.color,
    this.colorBlendMode,
  }) : super(key: key);

  final double? width;
  final double? height;
  final BoxFit fit;
  final String? url;
  final Map<String, String>? headers;
  final Alignment alignment;
  final Duration transformDuration;
  final double? radius;
  final BorderRadius? borderRadius;
  final ExtendedImageMode mode;
  final bool enableSlideOutPage;
  final Widget Function(Widget)? heroBuilderForSlidingPage;
  final String? heroTag;
  final Animation<double>? opacity;
  final Color? color;
  final BlendMode? colorBlendMode;

  @override
  Widget build(BuildContext context) {
    if (url == null || url!.trim().isEmpty) return _buildErrWidget('url is empty');

    late ImageProvider image;

    if (url!.startsWith('/')) {
      image = ExtendedFileImageProvider(File(url!));
    } else if (url!.startsWith('http')) {
      image = ExtendedNetworkImageProvider(url!, headers: headers);
    } else if (url!.startsWith('images/')) {
      image = ExtendedAssetImageProvider(url!);
    }

    Widget exImage = ExtendedImage(
      image: image,
      width: width,
      height: height,
      fit: fit,
      alignment: alignment,
      mode: mode,
      opacity: opacity,
      color: color,
      colorBlendMode: colorBlendMode,
      enableSlideOutPage: enableSlideOutPage,
      heroBuilderForSlidingPage: heroBuilderForSlidingPage,
      shape: (borderRadius == null && radius == 0) ? null : BoxShape.rectangle,
      clipBehavior: (borderRadius == null && radius == 0) ? Clip.none : Clip.antiAlias,
      isAntiAlias: true,
      borderRadius:
          (borderRadius == null && radius == null) ? null : (borderRadius ?? BorderRadius.circular(radius ?? 0)),
      loadStateChanged: (ExtendedImageState s) {
        switch (s.extendedImageLoadState) {
          case LoadState.loading:
            return _buildFadeInBox(s.completedWidget, 0);
          case LoadState.completed:
            return _buildFadeInBox(s.completedWidget, 1);
          case LoadState.failed:
            return _buildFadeInBox(_buildErrWidget('加载失败'), 1);
        }
      },
    );

    if (heroTag != null && heroTag!.isNotEmpty) exImage = Hero(tag: heroTag!, child: exImage);

    return exImage;
  }

  /// 构建透明过渡效果
  Widget _buildFadeInBox(Widget child, double opacity) {
    return AnimatedOpacity(
      opacity: opacity,
      duration: transformDuration,
      child: child,
    );
  }

  /// 加载失败视图
  Widget _buildErrWidget(String info) {
    return Container(
      alignment: Alignment.center,
      width: width,
      height: height,
      child: Text(info, style: const TextStyle(fontSize: 8)),
    );
  }
}
